<?php
namespace Mob\Model;

use Think\Model;
use Think\Hook;

/**数据字典模型
 * Class InfoModel
 */


class DataDictModel extends Model
{
	protected $tableName = 'configdatadict';


	/**
	 * createDataDict  创建数据字典
	 * @param $TypeId   数据字典类型ID
	 * @param $TypeDesc  类型描述
	 * @return 创建状态  true=成功 false=失败
	 * @author:wanghongyan
	 */

	public function setDataDict($TypeId,$TypeDesc)
	{
		$data['TYPEID']=strtoupper($TypeId);  //强制转换成大写
		$data['TYPEDESC']=$TypeDesc;
		return $this->add($data);
	}
	/**
	 * delDataDict  删除字典
	 * @param $TypeId   数据字典类型ID
	 * @return 创建状态  true=成功 false=失败
	 * @author:wanghongyan
	 */
	public function delDataDict($TypeId)
	{
	 $where['TYPEID']=strtoupper($TypeId);    //强制转换成大写
	 return $this->where($where)->delete();

	}
	 /**
	 * updateDataDict  更新字典
	 * @param $TypeId   数据字典类型ID
	 * @param $data    更新数据
	 * @return 更新状态  true=成功 false=失败
	 * @author:wanghongyan
	 */
	public function updateDataDict($Typeid,$data)
	{
	$where['TYPEID']=strtoupper($TypeId);    //强制转换成大写
	$this->where($where)->save($data);

	}
	/**
	 * isExistDataDict  查询字典是否存在
	 * @param $TypeId   数据字典类型ID
	 * @param $data    更新数据
	 * @return 更新状态  true=成功 false=失败
	 * @author:wanghongyan
	 */
	public function isExistDataDict($Typeid)
	{
	$where['TYPEID']=strtoupper($Typeid);    //强制转换成大写
	$where['ISSIMPLE']=1;

	return $this->where($where)->find();

	}
	/**
	 * getDataDict  取数据字典
	 * @param $TypeId   数据字典类型ID
	 * @return 数据字典信息  返回数据字典信息，如果没有则返回空
	 * @author:wanghongyan
	 */
	public function getDataDict($Typeid)
	{
	$where['TYPEID']=strtoupper($Typeid);    //强制转换成大写
	return $this->where($where)->field('TYPEID,TYPEDESC,ISSIMPLE')->find();
	}
	/**
	 * getDataItem  取数据项
	 * @param $TypeId   数据字典类型ID
	 * @return 数据字典信息  返回数据字典信息，如果没有则返回空
	 * @author:wanghongyan
	 */
	public function getDataItem($Typeid)
	{
	$res = $this->isExistDataDict($Typeid);

	if($res)
	{
	$publicdatadict  = M('publicdatadict');
	$where['TYPEID']=$Typeid;
	$where['STATUS']=1;
	return $publicdatadict->where($where)->field('TYPEID,TYPEDESC,DATAID,DATADESC')->select();

	}else
	{

	return null;
	}
	}

    /**
	 * setDataItem  增加数据字典项
	 * @param $TypeId   数据字典类型ID
	 * @return 数据字典信息  返回数据字典信息，如果没有则返回空
	 * @author:wanghongyan
	 */
	public function setDataItem($Typeid,$Typedesc='',$DataId,$DataDesc)
	{
	$res = $this->isExistDataDict($Typeid);

	if($res)
	{
	$data['TYPEID']=$Typeid;
	$data['TYPEDESC']=$Typedesc;
	$data['DATAID']=$DataId;
	$data['DATADESC']=$DataDesc;
	$publicdatadict  = M('publicdatadict');
	return $publicdatadict->add($data);

	}else
	{
	return false;
	}

	}
	public function  updateDataItem($TypeId,$data)
	{
	$where['TYPEID']=$TypeId;
	$publicdatadict  = M('publicdatadict');
	return $publicdatadict->where($where)->save($data);


	}

	public function getDataItemName($TypeId,$DataId)
	{
	 $datadict = M('publicdatadict');
	 $where['TYPEID']=$TypeId;
	 $where['DATAID']=$DataId;
	 $where['STATUS']=1;
	 return $datadict->where($where)->find()['DATADESC'];

	}





}